<?php

namespace src\models;
use PDO;
class Department
{
    private $pdo;

    public function __construct($pdo)
    {
        $this->pdo = $pdo;
    }

    //获取科室列表 (支持分页)
    public function getDepartment($limit, $offset, $whereClause, $params)
    {

        $stmt = $this->pdo->prepare("SELECT * FROM administrative $whereClause LIMIT :limit OFFSET :offset");
        foreach ($params as $key => $value) {
            $stmt->bindValue($key, $value, PDO::PARAM_STR);
        }
        $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
        $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    // 获取总记录数
    public function getTotalRows($whereClause = '', $params = [])
    {
        $stmt = $this->pdo->prepare("SELECT COUNT(*) as total FROM administrative $whereClause");

        //绑定参数
        foreach ($params as $key => $value) {
            $stmt->bindValue($key, $value, PDO::PARAM_STR);
        }

        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        return $result['total'];
    }

    // 添加科室
    public function addDepartment($DepartmentName, $DepartmentDescription)
    {
        $stmt = $this->pdo->prepare('INSERT INTO administrative (Departmentname, Departmentdescription) VALUES (:DepartmentName, :DepartmentDescription) ');
        return $stmt->execute(['DepartmentName' => $DepartmentName, 'DepartmentDescription' => $DepartmentDescription]);
    }

    //编辑科室
    public function editDepartment($id, $DepartmentName, $DepartmentDescription)
    {
        $stmt = $this->pdo->prepare('UPDATE administrative SET DepartmentName = :DepartmentName, DepartmentDescription = :DepartmentDescription WHERE id = :id');
        return $stmt->execute(['DepartmentName' => $DepartmentName, 'DepartmentDescription' => $DepartmentDescription, 'id' => $id]);
    }

    //删除科室
    public function deleteDepartment($id)
    {
        $stmt = $this->pdo->prepare('DELETE FROM administrative WHERE id = :id');
        return $stmt->execute(['id' => $id]);
    }

    // 获取单个科室信息
    public function getDepartmentById($id)
    {
        $stmt = $this->pdo->prepare('SELECT * FROM administrative WHERE id = :id');
        $stmt->execute(['id' => $id]);
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }
}

?>